﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using _180FrameWork.DataBaseHelper.Attributes;

namespace _180FrameWork.DataBaseHelper.Entities
{
    public class DataBaseTableSchedule
    {
        public string ClassName { get; set; }
        public string TableName { get; set; }
        private IList<DataBaseColumnSchedule> _ColumnList = null;
        public IList<DataBaseColumnSchedule> ColumnList
        {
            get
            {
                if (this._ColumnList == null)
                    this._ColumnList = new List<DataBaseColumnSchedule>();
                return this._ColumnList;
            }
        }

        public IList<DataBaseColumnSchedule> PrimaryKeyList
        {
            get
            {
                return this.ColumnList.Where(m => m.IsPrimaryKey == true).ToList();
            }
        }

        public IList<DataBaseColumnSchedule> ColumnNoPrimaryKeyList
        {
            get
            {
                return this.ColumnList.Where(m => m.IsPrimaryKey == false).ToList();
            }
        }
        public DataBaseTableSchedule(IList<DataBaseColumnSchedule> columnList)
        {
            this._ColumnList = columnList;
        }

        public DataBaseTableSchedule(Type type)
        {
            this.ClassName = type.Name;
            //通过自定义特性获取表名
            var tableAttribute = type.GetCustomAttributes(typeof(TableAttribute), false).FirstOrDefault();
            if (tableAttribute != null)
            {
                this.TableName = ((TableAttribute)tableAttribute).Name;
            }
            else
            {
                //通过类名取表名
                this.TableName = type.Name;
            }
            foreach (var field in type.GetProperties())
            {
                //是否忽略
                var ignoreAttribute = field.GetCustomAttributes(typeof(IgnoreAttribute), false).FirstOrDefault();
                if (ignoreAttribute == null)
                {
                    string columnName = "";
                    string propertieName = "";
                    var keyAttribute = field.GetCustomAttributes(typeof(KeyAttribute), false).FirstOrDefault();
                    if (keyAttribute != null)
                    {
                        propertieName = field.Name;
                        columnName = ((KeyAttribute)keyAttribute).Name;
                        if (columnName == null)
                        {
                            columnName = field.Name;
                        }
                        ColumnList.Add(new DataBaseColumnSchedule()
                        {
                            ColumnName = columnName,
                            PropertieName = propertieName,
                            IsPrimaryKey = true
                        });
                    }
                    else
                    {
                        var columnAttribute = field.GetCustomAttributes(typeof(ColumnAttribute), false).FirstOrDefault();
                        if (columnAttribute != null)
                        {
                            propertieName = field.Name;
                            columnName = ((ColumnAttribute)columnAttribute).Name;
                            if (columnName == null)
                            {
                                columnName = field.Name;
                            }
                            ColumnList.Add(new DataBaseColumnSchedule()
                            {
                                ColumnName = columnName,
                                PropertieName = propertieName,
                                IsPrimaryKey = false
                            });
                        }
                        else
                        {
                            propertieName = field.Name;
                            columnName = field.Name;
                            ColumnList.Add(new DataBaseColumnSchedule()
                            {
                                ColumnName = columnName,
                                PropertieName = propertieName,
                                IsPrimaryKey = false
                            });
                        }
                    }
                }
            }
        }
    }
}
